<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px">
      <el-form-item label="用户姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

<!--      <el-form-item label="业务员姓名" prop="agentName">-->
<!--        <el-input-->
<!--          v-model="queryParams.agentName"-->
<!--          placeholder="请输入业务员姓名"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="权证员姓名" prop="warrantName">-->
<!--        <el-input-->
<!--          v-model="queryParams.warrantName"-->
<!--          placeholder="请输入权证员姓名"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" border >
      <el-table-column label="ID" align="center" prop="id" width="100px" />
      <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="180px"
      />
      <el-table-column label="用户头像" align="center"  width="150px" >
        <template #default="scope">
          <img :src="scope.row.avatar" style="width: 60px;height: 60px;border-radius: 4px;">
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickname" width="150px" />
      <el-table-column label="用户姓名" align="center" prop="name" width="150px" />
      <el-table-column align="center" label="用户性别" prop="sex" width="100px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="mobile" width="140px" />
      <el-table-column label="身份证号" align="center" prop="identityCard" width="220px" />
<!--      <el-table-column label="业务员姓名" align="center" prop="agentName" width="150px" >-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--              link-->
<!--              v-if="!scope.row.agentName"-->
<!--              type="primary"-->
<!--              @click="clicktype=0;testfen();dialogVisible=true;alllocationDate.userId=scope.row.id;"-->
<!--          >-->
<!--            分配业务员-->
<!--          </el-button>-->
<!--        </template>-->

<!--      </el-table-column>-->
<!--      <el-table-column label="取消业务员分配" align="center" width="140px" >-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--              link-->
<!--              v-if="scope.row.agentName"-->
<!--              type="primary"-->
<!--              @click="clickcance=0;userid=scope.row.id;ConfCancAllo=true;"-->
<!--          >-->
<!--            取消业务员分配-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="权证员姓名" align="center" prop="warrantName" width="150px" >-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--              link-->
<!--              v-if="!scope.row.warrantName"-->
<!--              type="primary"-->
<!--              @click="clicktype=1;testfen();dialogVisible=true;alllocationDate.userId=scope.row.id;"-->

<!--          >-->
<!--            分配权证员-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="取消权证员分配" align="center" width="140px" >-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--              link-->
<!--              v-if="scope.row.warrantName"-->
<!--              type="primary"-->
<!--              @click="clickcance=1;userid=scope.row.id;ConfCancAllo=true;"-->
<!--          >-->
<!--            取消权证员分配-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" width="200px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm(scope.row.id)"

          >
            详情
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleDelete(scope.row.id)"
            checkPermi="['system:users:delete']"
          >
            删除
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleForbidden(scope.row.id)"
            checkPermi="['system:users:update']"
          >
            禁用
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <el-dialog
      title="分配"
      v-model="dialogVisible"
      center
      style="padding-top: 20px"
      width="500px">
    <el-table
        :data="salesmanList"
        style="width: 100%">
      <el-table-column
          prop="avatar"
          label="头像"
          width="60">
        <template #default="scope">
          <img :src="scope.row.avatar" style="width: 35px;height: 35px">
        </template>
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="用户"
        width="100">
      </el-table-column>
      <el-table-column
          prop="name"
          label="姓名"
          width="100">
      </el-table-column>
      <el-table-column
          prop="mobile"
          label="电话">
      </el-table-column>
      <el-table-column
          label="操作">
        <template #default="scope">
          <el-button
              link
              type="primary"

              @click="
              alllocationDate.agentId=scope.row.id;
              alllocationDate.agentName=scope.row.name;
              alllocationDate.warrantId=scope.row.id;
              alllocationDate.warrantName=scope.row.name;
              Allocateornot=true
              "
          >
            分配
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <el-dialog
      title="确认分配信息"
      v-model="Allocateornot"
      center
      style="width: 300px;padding-top: 20px;padding-bottom: 10px">
    <span slot="footer" class="dialog-footer" style="margin-left: 18%;" >
    <el-button @click="Allocateornot = false">取 消</el-button>
    <el-button type="primary" style="margin-left: 40px" @click="Allocateornot = false;dialogVisible=false;allocation()">确 定</el-button>
  </span>
  </el-dialog>
  <el-dialog
      title="确认取消分配"
      center
      style="padding-top: 20px;padding-bottom: 10px"
      v-model="ConfCancAllo"
      width="300px">
    <span slot="footer" class="dialog-footer" style="margin-left: 18%; ">
    <el-button @click="ConfCancAllo = false">取 消</el-button>
    <el-button style="margin-left: 40px" type="primary" @click="ConfCancAllo=false;Cancelsalesperson(userid)">确 定</el-button>
  </span>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { dateFormatter } from '@/utils/formatTime'
import * as UsersApi from '@/api/hutai/clientpool'
import { DICT_TYPE } from "@/utils/dict";
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
defineOptions({ name: 'HutaiClientpoolPool' })

//确认取消分配弹出窗
const  ConfCancAllo=ref(false)
//业务员列表弹出窗
const dialogVisible= ref(false)
//确认分配业务员窗口
const  Allocateornot =ref(false)
// 用户id
const  userid =ref()
// 点击的是分配业务员或者权证员
let clicktype=ref(0)
let clickcance=ref(0)
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
// 增加或者取消业务员结构
const alllocationDate = ref({
  userId: null,
  agentId: null,
  agentName: null,
  warrantId: null,
  warrantName: null
})

// 取消业务员函数
const  Cancelsalesperson = async (id) =>{
  alllocationDate.value.userId = id;
  const data =  alllocationDate.value;
  if (clickcance.value==0){
    await UsersApi.cancelAgent(data)
  }else {
    await UsersApi.cancelWarran(data)
  }
  getList()
  
}



//增加业务员函数
const putAgent= async ()=>{
  const data = alllocationDate.value
  await UsersApi.putAgent(data)
  getList()

}
const putWarrant= async ()=>{
  const data = alllocationDate.value

  await UsersApi.putWarrant(data)
  getList()

}


//增加业务员，增加权证员分配
const allocation=()=>{
  if(clicktype.value==0){
    alllocationDate.value.warrantId=0
    alllocationDate.value.warrantName=''
    putAgent()
  }else {
    alllocationDate.value.agentId=0
    alllocationDate.value.agentName=''
    putWarrant()
  }
  getList()

}


//获取业务员列表
const salesmanList=ref()
const testfen=()=>{
  if (clicktype.value==0){
    UsersApi.getAgentList().then(res=>{
      salesmanList.value=res
    })
  } else {
    UsersApi.getWarrantList().then(res=>{
      salesmanList.value=res
    })
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UsersApi.deleteUser(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const handleForbidden = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm('是否禁用当前用户')
    // 发起删除
    await UsersApi.forbiddenUser(id)
    message.success('操作成功')
    // 刷新列表
    await getList()
  } catch {}
}


/** 删除按钮操作 */
// const handleDelete = async (id: number) => {
//   try {
//     // 删除的二次确认
//     await message.delConfirm()
//     // 发起删除
//     await UsersApi.deleteUsers(id)
//     message.success(t('common.delSuccess'))
//     // 刷新列表
//     await getList()
//   } catch {}
// }


const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  username: null,
  password: null,
  nickname: null,
  customLabel: null,
  deptId: null,
  postIds: null,
  email: null,
  mobile: null,
  identityCard: null,
  sex: null,
  avatar: null,
  status: null,
  loginIp: null,
  loginDate: null,
  createTime: null,
  userType: null,
  agentId: null,
  agentName: null,
  remark: null,
  warrantId: null,
  warrantName: null,
  censusRegister: null,
  registeredHk: null,
  age: null,
  referrer: null,
  isHome: null,
  name: null,
  openId: null,
  wealthValue: null
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.userType = 1;
    const data = await UsersApi.getUsersPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const { push } = useRouter()
const openForm = (id) => {
  push('/clientUser/clientDetails?id='+id)
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
